/**
 * 项目申请历史记录模型
 * 记录项目申请的状态变更历史
 */

const BaseModel = require('../../../framework/database/model.js');

class ProjectApplyHistoryModel extends BaseModel {
    constructor() {
        super();
        
        // 设置集合名
        this.setCollection('project_apply_history');
        
        // 字段定义
        this.DB_STRUCTURE = {
            _id: 'string|true',
            APPLY_ID: 'string|true|comment=申请ID',
            EVENT_CODE: 'string|true|comment=事件代码',
            EVENT_NAME: 'string|true|comment=事件名称',
            FROM_STATUS: 'int|false|comment=原状态',
            FROM_STATUS_NAME: 'string|false|comment=原状态名称',
            FROM_ACTIVE_STATUS: 'int|false|comment=原活动状态',
            FROM_ACTIVE_STATUS_NAME: 'string|false|comment=原活动状态名称',
            TO_STATUS: 'int|true|comment=新状态',
            TO_STATUS_NAME: 'string|true|comment=新状态名称',
            TO_ACTIVE_STATUS: 'int|true|comment=新活动状态',
            TO_ACTIVE_STATUS_NAME: 'string|true|comment=新活动状态名称',
            OPERATOR_ID: 'string|true|comment=操作人ID',
            OPERATOR_NAME: 'string|true|comment=操作人名称',
            COMMENT: 'string|false|comment=备注',
            CREATE_TIME: 'int|true|comment=创建时间'
        };
    }
    
    /**
     * 添加历史记录
     * @param {Object} data 记录数据
     * @returns {Promise<String>} 记录ID
     */
    async insertHistory(data) {
        return await this.insert(data);
    }
    
    /**
     * 获取申请的历史记录
     * @param {String} applyId 申请ID
     * @param {Object} options 选项
     * @returns {Promise<Array>} 历史记录列表
     */
    async getHistoryByApplyId(applyId, options = {}) {
        const where = { APPLY_ID: applyId };
        const orderBy = options.orderBy || { CREATE_TIME: 1 };
        const page = options.page || 1;
        const size = options.size || 100;
        
        const result = await this.getList(where, page, size, orderBy);
        return result.list;
    }
    
    /**
     * 获取用户的历史操作记录
     * @param {String} userId 用户ID
     * @param {Object} options 选项
     * @returns {Promise<Array>} 历史记录列表
     */
    async getHistoryByUserId(userId, options = {}) {
        const where = { OPERATOR_ID: userId };
        const orderBy = options.orderBy || { CREATE_TIME: -1 };
        const page = options.page || 1;
        const size = options.size || 20;
        
        const result = await this.getList(where, page, size, orderBy);
        return result;
    }
    
    /**
     * 获取事件历史记录
     * @param {String} eventCode 事件代码
     * @param {Object} options 选项
     * @returns {Promise<Array>} 历史记录列表
     */
    async getHistoryByEvent(eventCode, options = {}) {
        const where = { EVENT_CODE: eventCode };
        const orderBy = options.orderBy || { CREATE_TIME: -1 };
        const page = options.page || 1;
        const size = options.size || 20;
        
        const result = await this.getList(where, page, size, orderBy);
        return result;
    }
    
    /**
     * 查询特定状态变更的历史记录
     * @param {Number} fromStatus 原状态
     * @param {Number} toStatus 新状态
     * @param {Object} options 选项
     * @returns {Promise<Array>} 历史记录列表
     */
    async getHistoryByStatusChange(fromStatus, toStatus, options = {}) {
        const where = { FROM_STATUS: fromStatus, TO_STATUS: toStatus };
        const orderBy = options.orderBy || { CREATE_TIME: -1 };
        const page = options.page || 1;
        const size = options.size || 20;
        
        const result = await this.getList(where, page, size, orderBy);
        return result;
    }
    
    /**
     * 获取时间范围内的历史记录
     * @param {Number} startTime 开始时间戳
     * @param {Number} endTime 结束时间戳
     * @param {Object} options 选项
     * @returns {Promise<Array>} 历史记录列表
     */
    async getHistoryByTimeRange(startTime, endTime, options = {}) {
        const where = { 
            CREATE_TIME: {
                $gte: startTime,
                $lte: endTime
            }
        };
        
        const orderBy = options.orderBy || { CREATE_TIME: 1 };
        const page = options.page || 1;
        const size = options.size || 100;
        
        const result = await this.getList(where, page, size, orderBy);
        return result;
    }
}

module.exports = ProjectApplyHistoryModel; 